package tech.wetech.admin3.controller;


import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tech.wetech.admin3.sys.service.AooAnotherMeService;
import tech.wetech.admin3.sys.service.AooAnotherSomeoneService;
import tech.wetech.admin3.sys.service.AooVirtualManService;
import tech.wetech.admin3.util.CustomResponse;
import tech.wetech.admin3.util.HashUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hashDuplicateChecking")
public class AooHashDuplicateCheckingController {
  private HashUtil hashUtil;
  private AooAnotherMeService anotherMeService;
  private AooAnotherSomeoneService anotherSomeoneService;
  private AooVirtualManService virtualManService;

  public AooHashDuplicateCheckingController(
    HashUtil hashUtil,
    AooAnotherMeService anotherMeService,
    AooAnotherSomeoneService anotherSomeoneService,
    AooVirtualManService virtualManService
  ){
    this.hashUtil = hashUtil;
    this.anotherMeService = anotherMeService;
    this.anotherSomeoneService = anotherSomeoneService;
    this.virtualManService = virtualManService;
  }

  @PostMapping("/file")
  public ResponseEntity<?> uploadFile(
    @RequestParam("file") MultipartFile file,
    @RequestParam("type") String type
  ) {
    String hash = hashUtil.getFileHash(file);
    boolean isDuplicate = false;
    Map<String, Object> data = new HashMap<>();

    switch (type) {
      case "image":
        isDuplicate = anotherMeService.isImageHashExist(hash)
          || anotherSomeoneService.isImageHashExist(hash)
          || virtualManService.isImageHashExist(hash);
        break;
      case "modelfile":
        isDuplicate = anotherMeService.isModelFileHashExist(hash)
          || anotherSomeoneService.isModelFileHashExist(hash)
          || virtualManService.isModelFileHashExist(hash);
        break;
      default:
        return ResponseEntity.badRequest().body(new CustomResponse.r1<>("参数无效", false, null));
    }

    data.put("duplicateChecking", isDuplicate);
    data.put("msg", isDuplicate ? "哈希重复" : "哈希唯一");
    data.put("hashValue", hash);
    return ResponseEntity.ok(new CustomResponse.r1<>("ok", true, data));
  }

//  @PostMapping("/image")
//  public ResponseEntity<?> imageDuplicateChecking(
//    @RequestParam("image") MultipartFile image
//  ){
//    String imageHash = hashUtil.getFileHash(image);
//    System.out.println(imageHash);
//    boolean duplicateChecking = anotherMeService.isImageHashExist(imageHash)
//      || anotherSomeoneService.isImageHashExist(imageHash)
//      || virtualManService.isImageHashExist(imageHash);
//
//    Map<String, Object> data = new HashMap<>();
//    data.put("duplicateChecking", duplicateChecking);
//    data.put("msg", duplicateChecking?"哈希重复":"哈希唯一");
//    data.put("hashValue", imageHash);
//    return ResponseEntity.ok(new CustomResponse.r1<>("ok", true, data));
//  }
}
